import * as THREE from 'three';
import { Vec3 } from '@/core/types';
import { Entity } from './Entity';
import { EntityType, AIState, EntityStats } from './types';
import { WorldManager } from '@/world/WorldManager';
import { ItemDropManager } from '@/world/ItemDrop';

export abstract class PassiveEntity extends Entity {
  protected wanderTarget?: Vec3;
  protected wanderRadius = 10;
  protected idleTime = 0;
  protected maxIdleTime = 3;

  constructor(id: string, type: EntityType, position: Vec3, worldManager: WorldManager, stats: EntityStats, itemDropManager?: ItemDropManager) {
    super(id, type, position, worldManager, stats, itemDropManager);
  }

  protected initializeAI(): void {
    this.ai = {
      state: AIState.IDLE,
      stateTimer: 0,
      lastStateChange: Date.now()
    };
  }

  protected processAI(deltaTime: number): void {
    if (!this.ai) return;

    switch (this.ai.state) {
      case AIState.IDLE:
        this.processIdleState(deltaTime);
        break;
      case AIState.WANDERING:
        this.processWanderingState(deltaTime);
        break;
      case AIState.DEAD:
        // 死亡状态不需要处理
        break;
    }
  }

  private processIdleState(deltaTime: number): void {
    this.idleTime += deltaTime;
    
    // 随机决定是否开始游荡
    if (this.idleTime >= this.maxIdleTime || Math.random() < 0.01) {
      this.startWandering();
    }
  }

  private processWanderingState(deltaTime: number): void {
    if (!this.wanderTarget) {
      this.startWandering();
      return;
    }

    // 移动向目标
    const distance = this.getDistanceTo(this.wanderTarget);
    
    if (distance < 1.0) {
      // 到达目标，切换到闲置状态
      this.ai!.state = AIState.IDLE;
      this.idleTime = 0;
      this.wanderTarget = undefined;
      this.velocity.x = 0;
      this.velocity.z = 0;
    } else {
      // 继续移动向目标
      this.moveTowards(this.wanderTarget, this.stats.moveSpeed);
    }

    // 如果游荡时间过长，停止游荡
    if (this.ai!.stateTimer > 10) {
      this.ai!.state = AIState.IDLE;
      this.idleTime = 0;
      this.wanderTarget = undefined;
      this.velocity.x = 0;
      this.velocity.z = 0;
    }
  }

  private startWandering(): void {
    if (!this.ai) return;

    // 选择一个随机的游荡目标
    const angle = Math.random() * Math.PI * 2;
    const distance = Math.random() * this.wanderRadius;
    
    this.wanderTarget = {
      x: this.position.x + Math.cos(angle) * distance,
      y: this.position.y,
      z: this.position.z + Math.sin(angle) * distance
    };

    this.ai.state = AIState.WANDERING;
    this.ai.stateTimer = 0;
    this.idleTime = 0;
  }

  protected onDamage(amount: number): void {
    // 友好生物受伤时会逃跑
    if (this.ai && this.ai.state !== AIState.FLEEING) {
      this.ai.state = AIState.FLEEING;
      this.ai.stateTimer = 0;
      
      // 随机选择一个逃跑方向
      const angle = Math.random() * Math.PI * 2;
      const fleeDistance = 15;
      
      this.wanderTarget = {
        x: this.position.x + Math.cos(angle) * fleeDistance,
        y: this.position.y,
        z: this.position.z + Math.sin(angle) * fleeDistance
      };
    }
  }

  protected onDeath(): void {
    // 友好生物死亡时可能掉落物品
    this.dropItems();
  }

  protected abstract dropItems(): void;

  // 友好生物可以被玩家吸引（比如用食物）
  public attractTo(target: Vec3): void {
    if (this.ai && this.ai.state !== AIState.DEAD) {
      this.ai.state = AIState.FOLLOWING;
      this.ai.target = target;
      this.ai.stateTimer = 0;
    }
  }
}